<?php 
header("Content-type: text/html; charset=utf-8"); 
require('../../config.php');  //数据库
$link = mysql_connect(DB_HOST,DB_USER,DB_PWD);
mysql_select_db(DB_NAME) or die('Could not select database');
_mysql_query("SET NAMES UTF8");

require('../../proxy_info.php');  //获取域名

$customer_id=$_POST["customer_id"];
$user_address=$_POST["user_address"];
//file_put_contents('/opt/www/weixin_platform/weixinpl/back_newshops/Product/product/address_log.txt' , date('Y-m-d H:i:s').$user_address.PHP_EOL , FILE_APPEND);
//$user_address=$_COOKIE["user_address"];

session_start();
$_SESSION['user_address']=$user_address;




$cat_id=-1;
//$owner_id=-1;
$pro_numshow=1;
$str->code=-1;
$i=0;

require('../../customer_id_decrypt.php'); //导入文件,获取customer_id_en[加密的customer_id]以及customer_id[已解密]
$new_baseurl ="//".$http_host;

$cat_id = $configutil->splash_new($_POST["foreign_id"]);
$pro_numshow = $configutil->splash_new($_POST["pro_numshow"]) ? : 0;

$query = "select id,name,p_area,default_imgurl,now_price,orgin_price,sell_count,show_sell_count,isvalid,issnapup,is_supply_id,storenum,is_QR,is_identity,is_virtual,pro_card_level_id,buystart_time,countdown_time,tihuo_time from weixin_commonshop_products where isvalid=true and isout=0 and customer_id=".$customer_id;	



$typeson_id=array();
	if($cat_id >0){
		// $type_son="select id from weixin_commonshop_types where isvalid=true and is_shelves=1 and parent_id=".$cat_id." and customer_id=".$customer_id."";
		// $result_typeson=_mysql_query($type_son) or die ('typeson faild ' .mysql_error());
		// while($row=mysql_fetch_object($result_typeson)){

			// $typeson_id[]=$row->id;
		// }
		/* 查找该分类的所有子分类 start */
		$query_child = "SELECT id FROM weixin_commonshop_types WHERE customer_id=".$customer_id." AND isvalid=true AND is_shelves=1 AND LOCATE(',".$cat_id.",', gflag)>0 ";
		$result_child = _mysql_query($query_child) or die('Query_child failed:'.mysql_error());
		while( $row_child = mysql_fetch_object($result_child) ){
			$child_id = $row_child -> id;
			
			$typeson_id[] = $child_id;
		}
		/* 查找该分类的所有子分类 end */
		if(empty($typeson_id)){
			$typeson_id=$cat_id; 
		}else{
			array_push($typeson_id,$cat_id);
			$typeson_id=implode(',',$typeson_id);
		}
			$query = $query." and (";
			$typeson_id_arr = explode(",",$typeson_id);
			$typeson_id_count = count($typeson_id_arr);
			for($j=0;$j<$typeson_id_count;$j++){
				$o_typeid = $typeson_id_arr[$j];
				if($j==0){
					$query = $query."( LOCATE(',".$o_typeid.",', type_ids)>0)";
					}else{
					$query = $query." or (LOCATE(',".$o_typeid.",', type_ids)>0)";
				}
			}
			$query = $query.")";
			unset($typeson_id);
		
	}	
	
	$query= $query." order by asort_value desc,id desc limit 0,".$pro_numshow;
	$result = _mysql_query($query) or die('Query failed211: ' . mysql_error());
	//echo $query;

	$num=0;
	$num=mysql_num_rows($result);
	if($num==0){   
		echo "{\"code\":\"'4003'\"}";
		return;	
	}
	
	while ($row = mysql_fetch_object($result)) {
		$area_flag = false;
        $p_area=$row->p_area;
		$p_area_arr = explode('-' , $p_area);
		foreach($p_area_arr as $p_area)
		{			
			if (empty($p_area) || $p_area == '全国') 
			{
				# code...
				$area_flag = true;
			}
			else
			{
				if (strpos($user_address, $p_area) == false) 
				{
				 }
				 else
				 {
					 $area_flag = true;
				 }
			}
		}
		if(!$area_flag)
		{
			continue;
		}
        
        


		$pid = $row->id;
		$name = $row->name;
		$default_imgurl = $row->default_imgurl;
		$now_price = $row->now_price;
		$orgin_price=$row->orgin_price;
//		$sell_count=$row->sell_count;
		$show_sell_count=$row->show_sell_count;
		$sq = "select sum(rcount) as coun from weixin_commonshop_orders where pid='".$pid."' and paystatus=1 and sendstatus not in(3,4,5,6)";
		$resultxx = mysql_query($sq);
		while ($rowxx = mysql_fetch_object($resultxx)) {
			 $sell_count = $rowxx->coun;
		}
//		$sell_count = DB::name('weixin_commonshop_orders')->where("pid='".$pid."' and paystatus=1")->count();
//		$count=$sell_count+$show_sell_count;
		$storenum = $row->storenum;
		
		
		
		$return[$i]['pid'] = $pid;
		$return[$i]['name'] = $name;
		$return[$i]['default_imgurl'] = $default_imgurl;
		$return[$i]['now_price'] = $now_price;
		$return[$i]['orgin_price'] = $orgin_price;
//		$count=$sell_count+$show_sell_count;
		$return[$i]['count'] =$sell_count + 0;
		$return[$i]['now_count'] =$storenum-$sell_count;
		$return[$i]['buystart_time'] =date("m-d H:i",strtotime($row->buystart_time));
		$return[$i]['base_buystart_time'] =strtotime($row->buystart_time);
		$return[$i]['countdown_time'] =date("m-d H:i",strtotime($row->countdown_time));
		$return[$i]['tihuo_time'] =$row->tihuo_time;
		$return[$i]['isvalid'] =$row->isvalid;
		$return[$i]['is_supply_id'] =$row->is_supply_id;
		$return[$i]['storenum'] =$row->storenum;
		$return[$i]['is_QR'] =$row->is_QR;
		$return[$i]['is_identity'] =$row->is_identity;
		$return[$i]['is_virtual'] =$row->is_virtual;
		$return[$i]['pro_card_level_id'] =$row->pro_card_level_id;
		if(empty($row->issnapup))
		{
			$return[$i]['buystart_time'] = 0;
			$return[$i]['countdown_time'] = 0;
		}
		$i++;
	}


	
$str="";
$str->code=0;
$str->num=$num;
$str->query=$query;
$str->msg=$return;
echo json_encode($str);	
?>